//引入重置样式
require('../css/commCss/normalize.css')
//引入重置扩展样式
require('../css/commCss/reset.css')
//引入公共样式
require('../css/commCss/property.css')
//引入公共扩展样式
require('../css/commCss/basic.css')

//引入当前页面样式
require('../css/login.less')

//引入axios
const axios=require('axios')
// console.log(axios);

//weui-插件库
require('weui')
const weui = require('weui.js')

//文档加载事件
document.addEventListener('DOMContentLoaded',function(){

    //0获取元素
    const account=document.querySelector('#account')
    const aginpassword=document.querySelector('#aginpassword')
    const registeraccount=document.querySelector('.registeraccount')
    const errmsg=document.querySelector('.errmsg')

    // console.log(account,aginpassword,registeraccount,errmsg);

    //1登录注册账号
    registeraccount.addEventListener('click',function(){
        console.log(account.value);
        if(!/^1[3-9][0-9]{9}$/.test(account.value)){
            errmsg.textContent = `账号不合法`
            return;
        } else if (!/^\d{3,6}$/.test(aginpassword.value)) {
            errmsg.textContent = `6-12:以字母开头,跟数字 字母 下划线`
            return;
        } else {
            //验证通过
            axios.post('http://139.9.177.51:8099/users/login', {
              account: account.value,
              password: aginpassword.value
            }).then(function (res) {
              console.log(res);
              if (res.data.status === 0) {
      
                //将用户id存入本地存储
                localStorage.setItem('userId', res.data.data.user.userId)
      
                weui.toast('登录成功', {//toast表示轻提示,并且操作成功是提示
                  duration: 1000, //多久后弹出轻提示框
                  //className: 'custom-classname', //自定义类名
                  callback: function () {//回调函数
                    location.href = './home.html'
                  }
                });
              } else {
                weui.alert(res.data.msg)//错误提示信息
              }
            })
      
          }
    })

   


})